package cn.yhjz.generator.controller;

import cn.yhjz.common.AjaxResponse;
import cn.yhjz.common.Pager;
import cn.yhjz.common.util.IdUtil;
import cn.yhjz.generator.entity.SysGenModel;
import cn.yhjz.generator.entity.SysGenTemplate;
import cn.yhjz.generator.service.ISysGenModelService;
import cn.yhjz.generator.service.ISysGenProjectService;
import cn.yhjz.generator.entity.SysGenProject;
import cn.yhjz.generator.service.ISysGenTemplateService;
import cn.yhjz.generator.util.TemplateUtil;
import cn.yhjz.generator.vo.SysGenProjectVo;

import cn.hutool.core.util.StrUtil;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/gen/project/")
@Slf4j
public class SysGenProjectController {

    @Autowired
    private ISysGenProjectService sysGenProjectService;
    @Autowired
    private ISysGenTemplateService templateService;
    @Autowired
    private ISysGenModelService modelService;
    @Autowired
    private TemplateUtil templateUtil;

    @PostMapping("/list")
    public AjaxResponse<List<SysGenProject>> list(@RequestBody SysGenProject sysGenProject) {
        return AjaxResponse.success(sysGenProjectService.select(sysGenProject));
    }

    @PostMapping("/page")
    public AjaxResponse page(@RequestBody SysGenProjectVo vo) {
        SysGenProject entity = new SysGenProject();
        BeanUtils.copyProperties(vo, entity);
        Pager pager = sysGenProjectService.getPage(vo.getCurrentPage(), vo.getPageSize(), entity);
        //获得其中每个项目下的所有模板和
        return AjaxResponse.success(pager);
    }

    @PostMapping("/addOrUpdate")
    public AjaxResponse add(@RequestBody SysGenProject entity) {
        if (entity.getId() != null) {
            return AjaxResponse.success(sysGenProjectService.updateById(entity));
        } else {
            entity.setProjectCode(IdUtil.generate());
            return AjaxResponse.success(sysGenProjectService.insert(entity));
        }
    }

    @GetMapping("/selectById")
    public AjaxResponse selectById(Long id) {
        return AjaxResponse.success(sysGenProjectService.selectById(id));
    }

    @DeleteMapping("/deleteById")
    public AjaxResponse deleteById(Long id) {
        return AjaxResponse.success(sysGenProjectService.deleteById(id));
    }

    /**
     * 生成所选项目所选表的所有代码
     *
     * @param projectCode
     * @return
     */
    @GetMapping("/generateAll")
    public AjaxResponse generateAll(String projectCode, String modelCode) throws TemplateException, IOException {
        if (StrUtil.isBlank(projectCode) || StrUtil.isBlank(modelCode)) {
            return AjaxResponse.fail("参数不够");
        }
        //查询项目中的所有模板
        SysGenTemplate templateExample = new SysGenTemplate();
        templateExample.setProjectCode(projectCode);
        List<SysGenTemplate> templateList = templateService.select(templateExample);
        //获得表
        SysGenModel modelExample = new SysGenModel();
        modelExample.setModelCode(modelCode);
        SysGenModel model = this.modelService.selectOne(modelExample);
        //为所有模板生成代码
        List<Map<String, String>> resList = new ArrayList<>();

        for (SysGenTemplate template : templateList) {
            String code = templateUtil.assemble(model, template);
            Map<String, String> resMap = new HashMap<>();
            resMap.put("templateName", template.getTemplateName());
            resMap.put("templateCode", code);
            resMap.put("sourceType", template.getSourceType());
            resList.add(resMap);
        }
        return AjaxResponse.success(resList);
    }

    /**
     * 生成所有代码到磁盘
     * 由于解决不了生成磁盘路径问题，所以此方法被弃用，待有更好的方案之后再实现
     * @param projectCode
     * @param modelCode
     */
    @Deprecated
    @GetMapping("generateAll2Disk")
    public AjaxResponse generateAll2Disk(String projectCode, String modelCode) throws TemplateException, IOException {
        if (StrUtil.isBlank(projectCode) || StrUtil.isBlank(modelCode)) {
            return AjaxResponse.fail("参数不够");
        }
        //查询项目中的所有模板
        SysGenTemplate templateExample = new SysGenTemplate();
        templateExample.setProjectCode(projectCode);
        List<SysGenTemplate> templateList = templateService.select(templateExample);
        //获得表
        SysGenModel modelExample = new SysGenModel();
        modelExample.setModelCode(modelCode);
        SysGenModel model = this.modelService.selectOne(modelExample);
        //为所有模板生成代码
        String basePath = System.getProperty("user.dir");
        for (SysGenTemplate template : templateList) {
            if (!("java".equalsIgnoreCase(template.getSourceType()) || "xml".equalsIgnoreCase(template.getSourceType()))) {
                continue;
            }
            String code = templateUtil.assemble(model, template);
            //根据模板规则生成文件名
            String fileName = templateUtil.assembleTemplate(model, template.getFilenameTemplate());
            log.info(fileName);
        }
        return AjaxResponse.success();
    }
}
